package com.wf.util;

import com.aliyun.facebody20191230.Client;
import com.aliyun.facebody20191230.models.*;
import com.aliyun.tea.TeaException;
import com.aliyun.tea.TeaModel;
import com.aliyun.teautil.models.RuntimeOptions;
import com.wf.dto.FaceDTO;
import com.wf.properties.AliYunProperties;
import com.wf.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

import static org.bouncycastle.crypto.tls.ConnectionEnd.client;

/**
 * @author mpk
 * @since 2024/10/12 11:23:15
 */
@Component
public class AliFaceUtil {

    private final AliYunProperties aliYunProperties;

    public AliFaceUtil(AliYunProperties aliYunProperties) {
        this.aliYunProperties = aliYunProperties;
    }

    @Autowired
    RedisUtil redisUtil;

    @Autowired
    Client client;

    /**
     * 添加人脸样本
     */
    public Boolean createFaceDb(FaceDTO faceDTO) {


        AddFaceEntityRequest addFaceEntityRequest = new com.aliyun.facebody20191230.models.AddFaceEntityRequest()
                .setDbName(faceDTO.getDbName())
                .setEntityId(faceDTO.getEntityId());
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            // 复制代码运行请自行打印 API 的返回值
            AddFaceEntityResponse addFaceEntityResponse = client.addFaceEntityWithOptions(addFaceEntityRequest, runtime);
            // 获取整体结果
            System.out.println(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(addFaceEntityResponse)));
            return true;
        } catch (TeaException teaException) {
            // 获取整体报错信息
            System.out.println(com.aliyun.teautil.Common.toJSONString(teaException));
            // 获取单个字段
            System.out.println(teaException.getCode());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return false;
    }



    /**
     * 添加人脸数据
     */
    public String addFace(FaceDTO faceDTO) {
        AddFaceRequest addFaceRequest = new AddFaceRequest()
                .setDbName(faceDTO.getDbName())
                .setImageUrl(faceDTO.getImageUrl())
                .setEntityId(faceDTO.getEntityId())
                .setExtraData(faceDTO.getExtraData());
        RuntimeOptions runtime = new RuntimeOptions();
        try {
            // 复制代码运行请自行打印 API 的返回值
            AddFaceResponse addFaceResponse = client.addFaceWithOptions(addFaceRequest, runtime);
            // 获取整体结果
            System.out.println(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(addFaceResponse)));
            // 获取单个字段
            System.out.println(addFaceResponse.getBody().getData().getFaceId());

            return null;
        } catch (TeaException teaException) {
            // 获取整体报错信息
            System.out.println(com.aliyun.teautil.Common.toJSONString(teaException));
            // 获取单个字段
            System.out.println(teaException.getCode());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return null;
    }

    /**
     * 人脸搜索
     */
    public Boolean searchFace(FaceDTO faceDTO) {
        SearchFaceRequest searchFaceRequest = new SearchFaceRequest()
                .setDbName(faceDTO.getDbName())
                .setLimit(2)
                .setImageUrl(faceDTO.getImageUrl());
        RuntimeOptions runtime = new RuntimeOptions();
        try {
            // 复制代码运行请自行打印 API 的返回值
            SearchFaceResponse searchFaceResponse = client.searchFaceWithOptions(searchFaceRequest, runtime);
            if(searchFaceResponse.statusCode==200)
            {
                List<SearchFaceResponseBody.SearchFaceResponseBodyDataMatchList> matchList = searchFaceResponse.getBody().getData().getMatchList();
            }

            // 获取整体结果
            System.out.println(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(searchFaceResponse)));
            // 获取单个字段：Confidence转换后的置信度
            if(  searchFaceResponse.getBody().getData().getMatchList().iterator().next().getFaceItems().iterator().next().getConfidence()>80.0)
            {
                return true;
            }

        } catch (TeaException teaException) {
            // 获取整体报错信息
            System.out.println(com.aliyun.teautil.Common.toJSONString(teaException));
            // 获取单个字段
            System.out.println(teaException.getCode());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return false;
    }

}
